package top.jolyoulu.sql

import org.apache.spark.rdd.RDD
import org.apache.spark.sql.{DataFrame, Dataset, Row, SparkSession}
import org.apache.spark.{SparkConf, SparkContext}

/**
 * @Author: JolyouLu
 * @Date: 2024/5/16 19:21
 * @Description
 */
object Spark01_SparkSQL_UDF {
  def main(args: Array[String]): Unit = {
    //创建SparkSQL的运行环境
    val sparkSQL: SparkConf = new SparkConf().setMaster("local[*]").setAppName("SparkSQL")
    val spark = SparkSession.builder().config(sparkSQL).getOrCreate()
    val sc: SparkContext = spark.sparkContext
    import spark.implicits._ //将spark转换规则引入
    //DataFrame转换
    val path: String = this.getClass.getClassLoader.getResource("datas/user.json").toURI.getPath
    val df: DataFrame = spark.read.json(path)
    df.createOrReplaceTempView("user")
    //自定义一个函数放入到spark中
    spark.udf.register("prefixName", (name: String) => "Name:" + name)
    //使用用户自定义函数
    spark.sql("select age,prefixName(username) from user").show()

    //关闭环境
    spark.close()
  }

  case class User(age: Long, username: String)
}
